package com.songminju.jsong_httpserver.http.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.songminju.jsong_httpserver.http.Cookie;
import com.songminju.jsong_httpserver.http.HttpRequest;

public class DefaultHttpRequest implements HttpRequest{
	
	private String uri;
	private String method;
	private Map<String,String> params = new HashMap<>();
	private Map<String,String> headers = new HashMap<>();
	private List<Cookie> cookies = new LinkedList<>();
	
	public static DefaultHttpRequest createRequest(BufferedReader br) {
		DefaultHttpRequest req = new DefaultHttpRequest();
		try {
			String[] strs = br.readLine().split(" ");
			req.method=strs[0];
			int x = strs[1].indexOf("?");
			if(x<0) {
				req.uri = strs[1];
			}else {
				req.uri = strs[1].substring(0, x);
				String paramStr = strs[1].substring(x+1);
				String[] paramsStr = paramStr.split("&");
				for(String pStr:paramsStr) {
					x = pStr.indexOf("=");
					if(x>0) {
						String key = pStr.substring(0,x);
						String value = pStr.substring(x);
						req.params.put(key, value);
					}
				}
			}
			while(true) {
				String line = br.readLine();
				if("".equals(line)) {
					break;
				}
				x = line.indexOf(":");
				req.headers.put(line.substring(0, x).trim(), line.substring(x+1).trim());
			}
			if("post".equalsIgnoreCase(req.method)) {
				
			}
			
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return req;
	}
	
	public DefaultHttpRequest() {
		
	}
	
	@Override
	public String uri() {
		return uri;
	}
	@Override
	public String method() {
		return method;
	}
	@Override
	public String param(String name) {
		return null;
	}
	@Override
	public List<String> paramValues(String name) {
		return null;
	}
	@Override
	public List<String> paramNames() {
		return null;
	}
	@Override
	public Cookie[] cookies() {
		return null;
	}
	@Override
	public Cookie cookie(String cookie) {
		return null;
	}
	@Override
	public String header(String name) {
		return null;
	}
	@Override
	public Map<String, String> headers() {
		return null;
	}
	
}
